<?php 
/**
 * Rma controller is for staff to create RMA
 * RMA controller is made by norman
 * most function is done
 * 
 * @author Norman
 *
 */
class RmaController extends Zend_Controller_Action
{  
	public static $rCenterMappRev;

    public function init(){   
		self::$rCenterMappRev = unserialize(ARR_RCENTER_REV_MAPPING);
	}

	/**
	 *This one is the page to create new RMA for shop 
	 *this one check barcode and staff password first
	 * @author Leo
	 * @version 2014-06-25
	 * Password confirm instead name input
	 */
	public function newAction(){
		
		$this->view->title = "Online RMA-New RMA";
		$this->view->headTitle($this->view->title,'PREPEND');
		
		$arrRepairCenter = self::$rCenterMappRev;
		
		
		//selection shop 
		$shopHead = $this->_getParam('shop');
		//echo $shopHead;
		if($shopHead!=""){
			
			$shops = new Model_DbTable_Shoplocation();
			$shopName = $shops->getNameByHead($shopHead);
			$this->view->rCenterKey = array_search($shopHead, $arrRepairCenter);
	
		}
		//echo "You are now login as ".$shopName['name_of_shop'];
		$this->view->shophead = $shopHead;
		if(isset($shopName)){
		$this->view->shopname = $shopName['name_of_shop'];
		}
		else{
		$this->_redirect("/rma/");
		$this->view->shopname = "<h1>Login ERROR</h1>";
		}
		
		
	}
	
	/**
	 * This is the second page in RMA system 
	 * by filling this page , the staff can submit the rma details
	 * This is page is not for parts rma and this page should not be visit stand alone
	 */
	public function fillAction(){
		$shophead = $_POST['shophead'];
		
		$this->view->title = "Online RMA-Fill In Details";
		$this->view->headTitle($this->view->title,'PREPEND');
		$sDetail = new Model_DbTable_Roster_Stafflogindetail();
		
		
		
		
		
		$productcode = trim($_POST['productcode']);
		$sCode = Model_System_Downloadhelper::downloadUrl('http://'.SERVER_IP_COMM.'/products/match-apos-bcode/bcode/'.$productcode);//
		$productcode = $sCode;
	
		//$staffname = $_POST['staffname'];
		$stLine = $sDetail->checkPasswordCorrect(Model_EncryptHelper::hashsha(trim($_POST["staffPassword"])));
		$staffName = $stLine['ni'];

		if(trim($shophead)=="" or trim($productcode) =="" or $stLine == false){
			if(!$stLine){
				$this->_redirect("/rma/index/password/no");
			}
			if(trim($productcode) ==""){
				$this->_redirect("/rma/index/barcode/no");
			}
			
			
		}
		
		
		
		
		$errorflag = 0;
		
		if($shophead!=""){
			
			$this->view->shophead = $shophead;			
		}
		else{
			
			$errorflag = 1;
		}
		
		if($productcode!=""){
				
			$this->view->productcode = $productcode;
		}
		else{
				
			$errorflag = 1;
		}		
		if($staffName!=""){
				
			$this->view->staffname = $staffName;
		}
		else{
				
			$errorflag = 1;
		}

		if($shophead == 'WHPC'){
			$this->renderScript("/rma/fill-wh.phtml");
		}
	}
	
	/**
	 * Step 3 in rma
	 * after the info is filled , it need finall double check and 
	 * then confirm and submit the result
	 */	
	public function confirmAction(){
		
		$this->view->title = "Online RMA-Confirm Your Submition";
		$this->view->headTitle($this->view->title,'PREPEND');
		
	}
	
	/**
	 * Step 4 in RMA
	 * this is the actural page to submit the RAM
	 */
	public function uploadresultAction(){
		/*
		echo "<pre>";
		var_dump($_POST);
		echo "</pre>";
		*/
		date_default_timezone_set('Australia/Melbourne');
		
		
		$this->view->dateToday = $dateToday = Model_DatetimeHelper::dateToday();
		$this->view->weekId = $weekId = date("W",strtotime($dateToday)) % 2;
		
		
		$this->view->title = "Online RMA-Save Submition";
		$this->view->headTitle($this->view->title);
		
		$productCode = $_POST['product_code'];
		$productName = $_POST['product_name'];
		$shopCode = $_POST['shop_head'];
		$staffName = $_POST['staff_name'];
		$qty = $_POST['product_qty'];
		$reasonFaulty = $_POST['faulty_reason'];
		$commFaulty = $_POST['faulty_comment'];
		$idInvoice = "";
		if(trim($_POST['invoice_number'])==""){
			
			$this->view->rmano = "ERROR OCCUR,MUST Input the Invoice Number.";
			return false;
		}
		else{

			if(!isset($_POST['invoice_number_faulty'])){
				$idInvoice=trim($_POST['invoice_number']);
			}
			else{
				$idInvoice=trim($_POST['invoice_number_faulty']);
			}
			
			
		}
		
		$dateSales = $_POST['product_sale_date'];
		$dateReturn = $_POST['customer_return_date'];		
		
		$newFaultyProduct = new Model_DbTable_Faultyproduct();
		$reason = new Model_DbTable_Faultyreason();
		$lastId = new Model_DbTable_Faultyproduct();
		
		$fn = $newFaultyProduct->listAllUnhandledFaultyByShopByDate($shopCode,Model_DatetimeHelper::getThisWeekMonday(), $dateToday);
		$this->view->faultyNo = count($fn) + 1;
		$rmareturn = $newFaultyProduct->addFaultyProduct(trim($productCode), $productName, $shopCode, $staffName, $qty, $reasonFaulty, $commFaulty, $idInvoice, $dateSales, $dateReturn);
		
		//20140625
		$reasonDes = $reason->getReasonDesByID($reasonFaulty);
		$this->view->reasonName = $reasonDes;
		$this->view->faultId = $rmareturn;
		
		if(isset($rmareturn)){
			$this->view->rmaID = $rmareturn;
			}
		else{
			$this->view->rmaID = "ERROR OCCUR";
			}	
		
		
	}
	/**
	 * The Rma summary page in the front page of RMA system 
	 * it shows the summary of the shop rma in this week or in last week
	 * it shows different week marker on the rma page so the staff 
	 * will not mes up for differnt weeks , it get less posibility of warehouse getting wrong week RMA
	 * so have to keep rma of this week
	 */			
	public function printsummaryAction(){
	
		$this->view->title = "Online RMA-Summary";
		$this->view->headTitle($this->view->title);
		
		$week = "this";
		$shopHead = $this->_getParam('shop');
		$week = $this->_getParam('week');
		
		$this->view->week = $week;
		
		date_default_timezone_set('Australia/Melbourne');
		
		$dateBegin = "";
		$dateEnd = "";
		$thisMonday = "";
		$thisSunday = "";
		$lastSunday = "";
		$lastMonday = "";
		
		if(intval(date("w"))==1){
			$thisMonday = date("Y-m-d",strtotime("this monday"));
		}
		else{
			$thisMonday = date("Y-m-d",strtotime("last monday"));
		}
		$thisSunday = date("Y-m-d",strtotime("+6 day",strtotime($thisMonday)));
		$lastSunday = date("Y-m-d",strtotime("-1 day",strtotime($thisMonday)));
		$lastMonday = date("Y-m-d",strtotime("-7 day",strtotime($thisMonday)));
		
		if($week == "this"){
			$dateBegin = $thisMonday;
			$dateEnd = $thisSunday;
			
		}
		elseif($week == "last"){
			$dateBegin = $lastMonday;
			$dateEnd = $lastSunday;			
			
		}
		
		$shopName ="";
		$newShopName = new Model_DbTable_Shoplocation();
		if($shopHead!=""){
			
			$shopName = $newShopName->getNameByHead($shopHead);
			$this->view->shopname = $shopName['name_of_shop'];	
		}
		
		$faultyProducts = new Model_DbTable_Faultyproduct();
		//$faultyResult = $faultyProducts->listAllUnhandledFaultyByShop($shopHead);
		$faultyResult = $faultyProducts->listAllUnhandledFaultyByShopByDate($shopHead, $dateBegin, $dateEnd);
		if($faultyResult){
			
			$this->view->faultyproducts = $faultyResult;
			
		}
		else{
			
			$this->view->faultyproducts = "There are no NEW RMA Recorder that unhandled";
		}
		
		$this->view->dateBegin = $dateBegin;
		$this->view->dateEnd = $dateEnd;
		
		
	}	
	
	/**
	 * It is possible that when Rma is done , it can not print the rma paper at that moment 
	 * this page is a link in the rma summary page , it shows the detail of rma so you may print it again 
	 * 
	 */
	public function reprintRmaAction(){
		
		$idRma = $this->_getParam("idrma");
		
		$rma = new Model_DbTable_Faultyproduct();
		$rList = $rma->getFaultyProduct($idRma);
		$reason = new Model_DbTable_Faultyreason();
		$reasonList = $reason->getReasonDesByID($rList['reason_faulty']);
		
		$this->view->weekId = $rList['week_of_year_faulty']%2;
		$this->view->rmaID = $rList['id_faulty'];
		$this->view->productCode = $rList['product_code_faulty'];
		$this->view->productDes = $rList['product_name_faulty'];
		$this->view->dateToday = $rList['date_faulty'];
		$this->view->shopHead = $rList['shopcode_faulty'];
		$this->view->staffName = $rList['staff_name_faulty'];
		$this->view->commentFaulty = $rList['comment_faulty'];
		//20140618
		$this->view->faultId = $rList['reason_faulty'];
		$this->view->faultReason = $reasonList;
		
		//$this->view->rList = $rList;
	}

			
}
?>